﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;

namespace Fayshine.VA.Models
{
    public class MonitorAreaGateExt : INotifyPropertyChanged
    {
        private int m_typeID;
        private int m_ID;
        private string m_Name;
        private int? m_ParentID;
        private bool m_IsSelected = false;
        private ObservableCollection<MonitorAreaGateExt> m_Childs = new ObservableCollection<MonitorAreaGateExt>();
        private MonitorAreaGateExt m_ParentMonitor = null;
        private bool m_ChangeChildSelection = true;

        /// <summary>
        /// 0: Gate; 1: Area;2: Areas
        /// </summary>
        public int TypeID
        {
            get
            {
                return m_typeID;
            }
            set
            {
                m_typeID = value;
                NofityPropertyChanged("TypeID");
            }
        }

        public int ID
        {
            get
            {
                return m_ID;
            }
            set
            {
                m_ID = value;
                NofityPropertyChanged("ID");
            }
        }

        public string Name
        {
            get
            {
                return m_Name;
            }
            set
            {
                m_Name = value;
                NofityPropertyChanged("Name");
            }
        }

        public int? ParentID
        {
            get
            {
                return m_ParentID;
            }
            set
            {
                m_ParentID = value;
                NofityPropertyChanged("ParentID");
            }
        }

        public bool IsSelected
        {
            get
            {
                return m_IsSelected;
            }
            set
            {
                if (m_IsSelected != value)
                {
                    m_IsSelected = value;

                    if (Childs.Count > 0 && m_ChangeChildSelection)
                    {
                        Childs.ToList().ForEach(c =>
                        {
                            c.IsSelected = value;
                        });
                    }
                    if (ParentMonitor != null)
                    {
                        ParentMonitor.RefreshSelection(false);
                    }
                    NofityPropertyChanged("IsSelected");
                }
            }
        }

        public ObservableCollection<MonitorAreaGateExt> Childs
        {
            get
            {
                return m_Childs;
            }
        }

        public MonitorAreaGateExt ParentMonitor
        {
            get
            {
                return m_ParentMonitor;
            }
            set
            {
                m_ParentMonitor = value;
                NofityPropertyChanged("ParentMonitor");
            }
        }

        public void RefreshSelection(bool changeChild)
        {
            m_ChangeChildSelection = changeChild;
            if (this.Childs.Count > 0)
            {
                if (this.Childs.Where(c => !c.IsSelected).Any())
                {
                    this.IsSelected = false;
                }
                else if (!this.Childs.Where(c => !c.IsSelected).Any())
                {
                    this.IsSelected = true;
                }
            }
            m_ChangeChildSelection = true;
        }

        #region INotifyPropertyChanged Interface

        public event PropertyChangedEventHandler PropertyChanged;
        public void NofityPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
